/*************************************************************************/
/** Copyright.															**/
/** FileName: GUI_List.c												**/
/** Author: XuYulin														**/
/** Version: 1.0.0.0													**/
/** Description: XML operations.										**/
/** History:															**/
/**	XuyYulin	2017/2/27	2.0.0.0		New create.						**/
/** XuYulin 2017/2/27 1.0 build this moudle								**/
/*************************************************************************/

//=======================================================================//
//= Include files.													    =//
//=======================================================================//
#include "LCD_OperationIF.h"
#include "GUI_Common.h"
#include "GUI_Basic.h"
#include "GUI_Font.h"
#include "GUI_ScrollBar.h"
#include "GUI_List.h"

#include <stddef.h>
#include <stdint.h>
//=======================================================================//
//= Static variable declaration.									    =//
//=======================================================================//
#define						LIST_FONT_SIZE					(FONT_SIZE_H12)

#define						LIST_TITLE_TEXT_POSX			(4)
#define						LIST_TITLE_TEXT_POSY			(4)
#define						LIST_TITLE_TEXT_AREA_WIDTH		(LCD_SIZE_WIDTH-LIST_TITLE_TEXT_POSX*2)
#define 					LIST_TITLE_TEXT_AREA_HEIGHT		(g_stFontSize[LIST_FONT_SIZE].Height)
static uint16_t				LIST_TITLE_LINE_POSX		=	(LIST_TITLE_TEXT_POSX+LIST_TITLE_TEXT_AREA_HEIGHT+1);



#define LIST_ITEM_POSX					(6)
#define LIST_ITEM_POSY(IDX)				(1+LIST_TITLE_LINE_POSX+(IDX*LIST_ITEM_HEIGHT))
#define LIST_ITEM_HEIGHT				(g_stFontSize[LIST_FONT_SIZE].Height+2)
#define LIST_ITEM_WIDTH					(LCD_SIZE_WIDTH-SCROLLBAR_BAR_WIDTH-LIST_ITEM_POSX*2)
#define LIST_ITEM_TEXT_WIDTH			(LIST_ITEM_WIDTH/2-1)
#define LIST_ITEM_VALUE_WIDTH			(LIST_ITEM_WIDTH/2-1)
#define LIST_ITEMS_AREA_HEIGHT			(LCD_SIZE_HEIGHT-LIST_TITLE_LINE_POSX-4)
#define LIST_ITEM_NUM_IN_PAGE			((LIST_ITEMS_AREA_HEIGHT-1)/LIST_ITEM_HEIGHT+1)
#define LIST_INTEGRAL_ITEM_NUM_IN_PAGE	(LIST_ITEMS_AREA_HEIGHT/LIST_ITEM_HEIGHT)
#define LIST_INTEGRAL_ITEMS_AREA_HEIGHT	(LIST_ITEM_NUM_IN_PAGE*LIST_ITEM_HEIGHT)


//=======================================================================//
//= Static function declaration.									    =//
//=======================================================================//

//=======================================================================//
//= Function implementation.										    =//
//=======================================================================//

void GUI_List_RefreshList(GUI_LIST_STRUCT* pstList)
{
	/*----------------------------------*/
	/* Variable Declaration				*/
	/*----------------------------------*/
	RECTANGLE	stTextDisplayArea;
	RECTANGLE	stTextDataArea;
	uint16_t	uiItemPosYOffSet;

	/*----------------------------------*/
	/* Initialize						*/
	/*----------------------------------*/

	/*----------------------------------*/
	/* Process							*/
	/*----------------------------------*/
	if(NULL != pstList)
	{
		// Draw frame
		GUI_Basic_DrawRectangle(0, 0, LCD_SIZE_WIDTH, LCD_SIZE_HEIGHT, GUI_COLOR_FRGCLR, GUI_COLOR_BKGCLR);
		GUI_Basic_DrawRectangle(2, 2, LCD_SIZE_WIDTH-4, LCD_SIZE_HEIGHT-4, GUI_COLOR_FRGCLR, GUI_COLOR_TRANS);
		GUI_Basic_DrawLine(3, LIST_TITLE_LINE_POSX, LCD_SIZE_WIDTH-4, LIST_TITLE_LINE_POSX, GUI_COLOR_FRGCLR);
		GUI_UpdateScrollBar(LCD_SIZE_WIDTH-2-SCROLLBAR_BAR_WIDTH, LIST_TITLE_LINE_POSX, LCD_SIZE_HEIGHT-2-LIST_TITLE_LINE_POSX, pstList->Count-1, pstList->SelectIndex);
		stTextDisplayArea.PosX = LIST_TITLE_TEXT_POSX;
		stTextDisplayArea.PosY = LIST_TITLE_TEXT_POSY;
		stTextDisplayArea.Width = LIST_TITLE_TEXT_AREA_WIDTH;
		stTextDisplayArea.Height = LIST_TITLE_TEXT_AREA_HEIGHT;
		stTextDataArea.PosX = 0;
		stTextDataArea.PosY = 0;
		GUI_Text_DrawSingleLineText(ENCODE(pstList->Title), LIST_FONT_SIZE, &stTextDisplayArea, &stTextDataArea, GUI_DRAW_NORMAL);
		// Draw list items
		if(pstList->SelectIndex < LIST_ITEM_NUM_IN_PAGE)
		{
			uiItemPosYOffSet = 0;
		}
		else
		{
			uiItemPosYOffSet = LIST_ITEMS_AREA_HEIGHT-LIST_INTEGRAL_ITEMS_AREA_HEIGHT;
		}
		GUI_Basic_DrawRectangle(LIST_ITEM_POSX, LIST_ITEM_POSY(0), LIST_ITEM_WIDTH, LIST_ITEMS_AREA_HEIGHT, GUI_COLOR_FRGCLR, GUI_COLOR_TRANS);
		GUI_Basic_DrawRectangle(LIST_ITEM_POSX+2, LIST_ITEM_POSY(0), LIST_ITEM_WIDTH-4, LIST_INTEGRAL_ITEMS_AREA_HEIGHT, GUI_COLOR_FRGCLR, GUI_COLOR_TRANS);

	}
}

void GUI_SelectUpItem(GUI_LIST_STRUCT* pstList)
{
	if(LIST_STATUS_SELECT == pstList->Mode)
	{
		if(pstList->SelectIndex > 0)
		{
			pstList->SelectIndex--;
			GUI_UpdateScrollBar(LCD_SIZE_WIDTH-2-SCROLLBAR_BAR_WIDTH, LIST_TITLE_LINE_POSX, LCD_SIZE_HEIGHT-2-LIST_TITLE_LINE_POSX, pstList->Count-1, pstList->SelectIndex);
		}
	}
}

void GUI_SelectDownItem(GUI_LIST_STRUCT* pstList)
{
	if(LIST_STATUS_SELECT == pstList->Mode)
	{
		if(pstList->SelectIndex < pstList->Count-1)
		{
			pstList->SelectIndex++;
			GUI_UpdateScrollBar(LCD_SIZE_WIDTH-2-SCROLLBAR_BAR_WIDTH, LIST_TITLE_LINE_POSX, LCD_SIZE_HEIGHT-2-LIST_TITLE_LINE_POSX, pstList->Count-1, pstList->SelectIndex);
		}
	}
}

void GUI_ChangeMode(GUI_LIST_STRUCT* pstList)
{
	if(LIST_STATUS_SELECT == pstList->Mode)
	{
		pstList->Mode = LIST_STATUS_EDIT;
	}
	else
	{
		pstList->Mode = LIST_STATUS_SELECT;
	}
}
